package com.example.a7822.myapplication.shopcart;

import android.util.Log;

import com.example.a7822.myapplication.shopcart.beans.GoodBean;
import com.example.a7822.myapplication.utils.Logger;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * @date 创建时间：2017/6/12
   @auther 创建人：洪坤峰
   @description：购物车相关信息管理类，单例模式
 */

public class SPCartManager {

    private String PaySuccessId;
    private JSONObject tempInfo;
    private String deleteId;
    private String changeId;
    private String changeNum;
    private double totalPrice = 0;
    private double totalGoodsPrice = 0;
    private int totalCount = 0;



    private String curShopID ;//当前商店ID
    private String orderNum="";//订单ID
//    private Address address;
    private Logger logger = Logger.getLogger(SPCartManager.class);


//    public Address getAddress() {
//        return address;
//    }
//
//    public void setAddress(Address address) {
//        this.address = address;
//    }

    private List<GoodBean> goodList = new ArrayList<>();
//    private List<CouponEntity> couponList = new ArrayList<>();
//    private List<CouponEntity> selectedCouponList = new ArrayList<>();

//    public List<CouponEntity> getSelectedCouponList() {
//        selectedCouponList.clear();
//        for (CouponEntity coupon :
//                couponList) {
//            if (coupon.isSelected()){
//                selectedCouponList.add(coupon);
//            }
//
//        }
//        return selectedCouponList;
//    }

//    public void setSelectedCouponList(List<CouponEntity> selectedCouponList) {
//        this.selectedCouponList = selectedCouponList;
//    }

//    public List<CouponEntity> getCouponList() {
//        return couponList;
//    }
//
//    public void setCouponList(List<CouponEntity> couponList) {
//        this.couponList = couponList;
//    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public String getCurShopID() {
        return curShopID;
    }

    public void setCurShopID(String curShopID) {
        this.curShopID = curShopID;
    }

    public static SPCartManager getInstance() {
        return instance;
    }

    private static SPCartManager instance = new SPCartManager();

    public String getOrderNum() {
        return orderNum;
    }

    public void setOrderNum(String orderNum) {
        this.orderNum = orderNum;
    }

    public String getDeleteId() {
        return deleteId;
    }

    public void setDeleteId(String deleteId) {
        this.deleteId = deleteId;
    }

    /**
     * 返回订单商品总价【未打折】
     * @return
     */
    public double getTotalGoodsPrice() {
        //计算商品总价
        Log.d("get price", "get total price");
        totalGoodsPrice = 0;
        for(GoodBean good : goodList){
            totalGoodsPrice += good.getGoodPriceNow() * good.getGoodAmount();

        }
        return totalGoodsPrice;
    }

    public void setTotalGoodsPrice(double totalGoodsPrice) {
        this.totalGoodsPrice = totalGoodsPrice;
    }

    /**
     * 返回订单最终价格【折扣价】
     * @return
     */
    public double getTotalPrice() {

        //计算商品总价
        Log.d("get price", "get total price");
        totalPrice = 0;
        for(GoodBean good : goodList){
            totalPrice += good.getGoodPriceNow() * good.getGoodAmount();

        }
        //商品价格
        Double goodBizPrice = totalPrice;
        //计算折扣
//        for (CouponEntity couponEntity : couponList){
//            logger.d("couponList有值");
//            if (couponEntity.isSelected()){
//                switch (couponEntity.getType()){
//                    //折扣券
//                    case 0:
//                        double discout = Double.parseDouble(couponEntity.getValue());
//                        totalPrice = totalPrice*discout;
//                        break;
//                    //满减券
//                    case 1:
//
//                        break;
//                    //抵用券
//                    case 2:
//                        double downValue = Double.parseDouble(couponEntity.getValue());
//                        totalPrice -= downValue;
//                        break;
//                }
//            }
//        }


        return totalPrice;
    }
    public int getTotalCount() {

        Log.d("get price", "get total price");
        totalPrice = 0;
        totalCount = 0;
        for(GoodBean good : goodList){
            totalCount += good.getGoodAmount();
        }

        return totalCount;
    }


    public String getPaySuccess() {
        return PaySuccessId;
    }

    public void setPaySuccess(String paySuccess) {
        PaySuccessId = paySuccess;
    }


    /**
     * 设置商品数量
     * @param goodId
     * @param goodNum
     */
    public void setGoodNumAndPrice(int goodId, int goodNum) {

        GoodBean good = getGood(goodId);
        good.setGoodAmount(goodNum);
        //设置商品单价
        good.setGoodTotalPrice();

    }

    public void setGoodPrice(int goodId) {

        GoodBean good = getGood(goodId);
        //设置商品单价
        good.setGoodTotalPrice();

    }

    /**
     * 获取指定id的商品
     * @param goodId
     * @return
     */
    public GoodBean getGood(int goodId){

        GoodBean goodBean = null;

        for (GoodBean one : goodList){
            if (one.getGoodId() == goodId){
                goodBean = one;
                break;
            }
        }
        return goodBean;

    }

    public void setGoodList(List<GoodBean> goodList) {
        this.goodList = goodList;
    }

    public String getAddGoodBeanCode() {
        return addGoodBeanCode;
    }

    public List<GoodBean> getGoodList() {
        return goodList;
    }

    public void setAddGoodBeanCode(String addGoodBeanCode) {
        this.addGoodBeanCode = addGoodBeanCode;
    }

    private String addGoodBeanCode;


    public void removeGood(GoodBean item) {
        goodList.remove(item);
    }

    /**
     * 清空数据
     */
    public void clearAll(){
        goodList.clear();
//        couponList.clear();
        totalCount = 0;
        totalPrice = 0;

    }


}
